package cst.a931xmc.yuejian20.Model;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.ActivityBean;
import cst.a931xmc.yuejian20.Bean.ActivityOrderBean;
import cst.a931xmc.yuejian20.Bean.AppBean;
import cst.a931xmc.yuejian20.Bean.AppnumUpdateBean;
import cst.a931xmc.yuejian20.Interface.ItemIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.HomeService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ActivityModel<T> extends ApiModel implements ItemIface {
    private Retrofit retrofit;
    private HomeService service;

    public ActivityModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(HomeService.class);

    }

    @Override
    public void getList(final Listener l) {
        Call<List<ActivityBean>> call = service.getActivityList();
        call.enqueue(new Callback<List<ActivityBean>>() {
            @Override
            public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ActivityBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getDetail(int activity_id, final Listener l) {
        Call<ActivityBean> call = service.getActivityDetail(activity_id);
        call.enqueue(new Callback<ActivityBean>() {
            @Override
            public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<ActivityBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void updateActivityAppNum(int activity_id, int activity_appnum){
        Call<AppnumUpdateBean> call = service.updateActivityAppNum(activity_id,activity_appnum);
        call.enqueue(new Callback<AppnumUpdateBean>() {
            @Override
            public void onResponse(Call<AppnumUpdateBean> call, Response<AppnumUpdateBean> response) {
                response.body();
            }

            @Override
            public void onFailure(Call<AppnumUpdateBean> call, Throwable t) {
                t.toString();
            }
        });
    }

    public void order(int activity_id, String activity_name, String activity_pic, String activity_intro,
                      int user_id, String aorder_rname, String aorder_tel, String aorder_idcard,
                      int aorder_num, String aorder_price, final Listener l) {
        Call<AppBean> call = service.order(activity_id,activity_name,activity_pic,activity_intro,user_id,aorder_rname,aorder_tel,aorder_idcard,aorder_num,aorder_price);
        call.enqueue(new Callback<AppBean>() {
            @Override
            public void onResponse(Call<AppBean> call, Response<AppBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<AppBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void getOrderList(int user_id, final Listener l) {
        Call<List<ActivityOrderBean>> call = service.getActivityorderList(user_id);
        call.enqueue(new Callback<List<ActivityOrderBean>>() {
            @Override
            public void onResponse(Call<List<ActivityOrderBean>> call, Response<List<ActivityOrderBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ActivityOrderBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void getOrderDetail(int aorder_id, final Listener l) {
        Call<ActivityOrderBean> call = service.getActivityorderDetail(aorder_id);
        call.enqueue(new Callback<ActivityOrderBean>() {
            @Override
            public void onResponse(Call<ActivityOrderBean> call, Response<ActivityOrderBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<ActivityOrderBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
